package com.zhuchaooasystem.adapter;

import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.zhuchaooasystem.R;

import java.util.List;

/**
 * Created by zhangyajiang on 2016/12/29.
 */

public class ExpandableAdapter extends BaseExpandableListAdapter {

    private List<String> groupList;//外层的数据源
    private List<List<String>> childList;//里层的数据源
    private Context context;

    //用来装载某个item是否被选中
    SparseBooleanArray selected;
    int old = -1;
    int parentPosition = -1;

    public ExpandableAdapter(Context context, List<String> groupList, List<List<String>> childList) {
        this.groupList = groupList;
        this.childList = childList;
        this.context = context;

        selected = new SparseBooleanArray();
    }


    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    /**
     * 这个返回的一定要是对应外层的item里面的List集合的size
     *
     * @param groupPosition
     * @return
     */
    @Override
    public int getChildrenCount(int groupPosition) {
        return childList.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupPosition;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childList.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        convertView = View.inflate(context, R.layout.expandable_header, null);
        //分组名字
        TextView textView = (TextView) convertView.findViewById(R.id.expendable_header_tv);

        textView.setText(groupList.get(groupPosition));

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup viewGroup) {
        view = View.inflate(context, R.layout.expandable_child, null);
        TextView textView = (TextView) view.findViewById(R.id.expendable_child_tv);
        //设置子选项的字体颜色
        if (groupPosition == 0) {
            if (selected.get(childPosition) && this.parentPosition == 0) {
                textView.setTextColor(context.getResources().getColor(R.color.color_19c4ad));
            } else {
                textView.setTextColor(context.getResources().getColor(R.color.color_7f7f7f));
            }
        }
        if (groupPosition == 1) {
            if (selected.get(childPosition) && this.parentPosition == 1) {
                textView.setTextColor(context.getResources().getColor(R.color.color_19c4ad));
            } else {
                textView.setTextColor(context.getResources().getColor(R.color.color_7f7f7f));
            }
        }


        //外层的分组名字
        textView.setText(childList.get(groupPosition).get(childPosition));
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    public void setSelectedItem(int groupPosition, int selected) {
        this.parentPosition = groupPosition;
        if (old != -1) {
            this.selected.put(old, false);
        }
        this.selected.put(selected, true);
        old = selected;
    }
}
